home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / multipad.zip / MPPRINT.C < prev    next >
Text File  |  1992-07-17  |  11KB  |  382 lines

  1. /***************************************************************************
  2.  *                                       *
  3.  *  MODULE    : MpPrint()                           *
  4.  *                                       *
  5.  *  PURPOSE    : Printing code for MultiPad.                   *
  6.  *                                       *
  7.  *  FUNCTIONS    : GetPrinterDC ()       -  Creates a printer DC for the *
  8.  *                          default device.           *
  9.  *                                       *
  10.  *          AbortProc ()           -  Export proc. for GDI to check*
  11.  *                          print abort.           *
  12.  *                                       *
  13.  *          PrintDlgProc ()       -  Dialog function for the print*
  14.  *                          cancel dialog.           *
  15.  *                                       *
  16.  *          PrintFile ()           -  Prints the contents of the   *
  17.  *                          edit control.           *
  18.  *                                       *
  19.  *          GetInitializationData () -  Gets DC initialisation data  *
  20.  *                          from a DC supporting       *
  21.  *                          ExtDeviceMode().           *
  22.  *                                       *
  23.  ***************************************************************************/
  24. // COPYRIGHT:
  25. //
  26. //   (C) Copyright Microsoft Corp. 1992.  All rights reserved.
  27. //
  28. //   You have a royalty-free right to use, modify, reproduce and
  29. //   distribute the Sample Files (and/or any modified version) in
  30. //   any way you find useful, provided that you agree that
  31. //   Microsoft has no warranty obligations or liability for any
  32. //   Sample Application Files which are modified.
  33.  
  34. #include "multipad.h"
  35. #include "commdlg.h"
  36.  
  37. PRINTDLG pd;      /* Common print dialog structure */
  38. BOOL fAbort;        /* TRUE if the user has aborted the print job     */
  39. HWND hwndPDlg;        /* Handle to the cancel print dialog         */
  40. PSTR szTitle;        /* Global pointer to job title             */
  41. HANDLE hInitData=NULL;    /* handle to initialization data         */
  42.  
  43.  
  44. /****************************************************************************
  45.  *                                        *
  46.  *  FUNCTION   : GetPrinterDC ()                        *
  47.  *                                        *
  48.  *  PURPOSE    : Creates a printer display context for the printer  *
  49.  *                                        *
  50.  *  RETURNS    : HDC   - A handle to printer DC.                *
  51.  *                                        *
  52.  ****************************************************************************/
  53. HDC FAR PASCAL GetPrinterDC(void)
  54. {
  55.  
  56.     HDC         hDC;
  57.     LPDEVMODE   lpDevMode = NULL;
  58.     LPDEVNAMES  lpDevNames;
  59.     LPSTR       lpszDriverName;
  60.     LPSTR       lpszDeviceName;
  61.     LPSTR       lpszPortName;
  62.  
  63.     if (!PrintDlg((LPPRINTDLG)&pd))
  64.         return(NULL);
  65.  
  66.     if (pd.hDC)
  67.       {
  68.         hDC = pd.hDC;
  69.       }
  70.     else
  71.       {
  72.  
  73.         if (!pd.hDevNames)
  74.             return(NULL);
  75.  
  76.         lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
  77.         lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset;
  78.         lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset;
  79.         lpszPortName   = (LPSTR)lpDevNames + lpDevNames->wOutputOffset;
  80.         GlobalUnlock(pd.hDevNames);
  81.  
  82.         if (pd.hDevMode)
  83.             lpDevMode = (LPDEVMODE)GlobalLock(pd.hDevMode);
  84.  
  85.         hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode);
  86.  
  87.         if (pd.hDevMode && lpDevMode)
  88.             GlobalUnlock(pd.hDevMode);
  89.       }
  90.  
  91.     if (pd.hDevNames)
  92.         GlobalFree(pd.hDevNames);
  93.     if (pd.hDevMode)
  94.         GlobalFree(pd.hDevMode);
  95.     return(hDC);
  96. }
  97.  
  98.  
  99. /****************************************************************************
  100.  *                                        *
  101.  *  FUNCTION   : AbortProc()                            *
  102.  *                                        *
  103.  *  PURPOSE    : To be called by GDI print code to check for user abort.    *
  104.  *                                        *
  105.  ****************************************************************************/
  106. int FAR PASCAL AbortProc ( hdc, reserved )
  107. HDC hdc;
  108. WORD reserved;
  109. {
  110.     MSG msg;
  111.  
  112.     /* Allow other apps to run, or get abort messages */
  113.     while (!fAbort && PeekMessage (&msg, NULL, NULL, NULL, TRUE))
  114.     if (!hwndPDlg || !IsDialogMessage (hwndPDlg, &msg)){
  115.         TranslateMessage (&msg);
  116.         DispatchMessage  (&msg);
  117.     }
  118.     return !fAbort;
  119. }
  120.  
  121. /****************************************************************************
  122.  *                                        *
  123.  *  FUNCTION   : PrintDlgProc ()                        *
  124.  *                                        *
  125.  *  PURPOSE    : Dialog function for the print cancel dialog box.        *
  126.  *                                        *
  127.  *  RETURNS    : TRUE  - OK to abort/ not OK to abort                *
  128.  *         FALSE - otherwise.                        *
  129.  *                                        *
  130.  ****************************************************************************/
  131. BOOL FAR PASCAL PrintDlgProc(HWND hwnd, WORD msg, WORD wParam, LONG lParam)
  132. {
  133.    switch (msg){
  134.        case WM_COMMAND:
  135.            /* abort printing if the only button gets hit */
  136.            fAbort = TRUE;
  137.            break;
  138.  
  139.     default:
  140.         return FALSE;
  141.     }
  142.     return TRUE;
  143. }
  144.  
  145. /****************************************************************************
  146.  *                                        *
  147.  *  FUNCTION   : PrintFile ()                            *
  148.  *                                        *
  149.  *  PURPOSE    : Prints the contents of the edit control.            *
  150.  *                                        *
  151.  ****************************************************************************/
  152.  
  153. VOID FAR PASCAL PrintFile(HWND hwnd)
  154. {
  155.     HDC     hdc;
  156.     int     yExtPage;
  157.     char    sz[32];
  158.     WORD    cch;
  159.     WORD    ich;
  160.     PSTR    pch;
  161.     WORD    iLine;
  162.     WORD    nLinesEc;
  163.     HANDLE  hT;
  164.     FARPROC lpfnAbort;
  165.     FARPROC lpfnPDlg;
  166.     HWND    hwndPDlg;
  167.     WORD    dy;
  168.     int     yExtSoFar;
  169.     WORD    fError = TRUE;
  170.     HWND    hwndEdit;
  171.  
  172.     hwndEdit = (HWND)GetWindowWord(hwnd,GWW_HWNDEDIT);
  173.  
  174.     /* Create the job title by loading the title string from STRINGTABLE */
  175.     cch = LoadString (hInst, IDS_PRINTJOB, sz, sizeof(sz));
  176.     szTitle = sz + cch;
  177.     cch += GetWindowText (hwnd, sz + cch, 32 - cch);
  178.     sz[31] = 0;
  179.  
  180.     /* Make instances of the Abort proc. and the Print dialog function */
  181.     lpfnAbort = MakeProcInstance (AbortProc, hInst);
  182.     if (!lpfnAbort)
  183.     goto getout;
  184.     lpfnPDlg = MakeProcInstance (PrintDlgProc, hInst);
  185.     if (!lpfnPDlg)
  186.     goto getout4;
  187.  
  188.     /* Initialize the printer */
  189.     hdc = GetPrinterDC();
  190.     if (!hdc)
  191.     goto getout5;
  192.  
  193.     /* Disable the main application window and create the Cancel dialog */
  194.     EnableWindow (hwndFrame, FALSE);
  195.     hwndPDlg = CreateDialog (hInst, IDD_PRINT, hwnd, lpfnPDlg);
  196.     if (!hwndPDlg)
  197.     goto getout3;
  198.     ShowWindow (hwndPDlg, SW_SHOW);
  199.     UpdateWindow (hwndPDlg);
  200.  
  201.     /* Allow the app. to inform GDI of the escape function to call */
  202.     if (Escape (hdc, SETABORTPROC, 0, (LPSTR)lpfnAbort, NULL) < 0)
  203.     goto getout1;
  204.  
  205.     /* Initialize the document */
  206.     if (Escape (hdc, STARTDOC, cch, (LPSTR)sz, NULL) < 0)
  207.     goto getout1;
  208.  
  209.     /* Get the height of one line and the height of a page */
  210.     dy = HIWORD (GetTextExtent (hdc, "CC", 2));
  211.     yExtPage = GetDeviceCaps (hdc, VERTRES);
  212.  
  213.     /* Get the lines in document and and a handle to the text buffer */
  214.     iLine     = 0;
  215.     yExtSoFar = 0;
  216.     nLinesEc  = (WORD)SendMessage (hwndEdit, EM_GETLINECOUNT, 0, 0L);
  217.     hT          = (HANDLE)SendMessage (hwndEdit, EM_GETHANDLE, 0, 0L);
  218.  
  219.     /* While more lines print out the text */
  220.     while (iLine < nLinesEc){
  221.     if (yExtSoFar + (int)dy > yExtPage){
  222.         /* Reached the end of a page. Tell the device driver to eject a
  223.          * page
  224.          */
  225.         if (Escape (hdc, NEWFRAME, 0, NULL, NULL) < 0 || fAbort)
  226.         goto getout2;
  227.         yExtSoFar = 0;
  228.     }
  229.  
  230.     /* Get the length and position of the line in the buffer
  231.      * and lock from that offset into the buffer */
  232.     ich = (WORD)SendMessage (hwndEdit, EM_LINEINDEX, iLine, 0L);
  233.     cch = (WORD)SendMessage (hwndEdit, EM_LINELENGTH, ich, 0L);
  234.     pch = LocalLock(hT) + ich;
  235.  
  236.     /* Print the line and unlock the text handle */
  237.     TextOut (hdc, 0, yExtSoFar, (LPSTR)pch, cch);
  238.     LocalUnlock (hT);
  239.  
  240.     /* Test and see if the Abort flag has been set. If yes, exit. */
  241.     if (fAbort)
  242.         goto getout2;
  243.  
  244.     /* Move down the page */
  245.     yExtSoFar += dy;
  246.     iLine++;
  247.     }
  248.  
  249.     /* Eject the last page. */
  250.     if (Escape (hdc, NEWFRAME, 0, NULL, NULL) < 0)
  251.     goto getout2;
  252.  
  253.     /* Complete the document. */
  254.     if (Escape (hdc, ENDDOC, 0, NULL, NULL) < 0){
  255. getout2:
  256.     /* Ran into a problem before NEWFRAME? Abort the document */
  257.     Escape( hdc, ABORTDOC, 0, NULL, NULL);
  258.     }
  259.     else
  260.     fError=FALSE;
  261.  
  262. getout3:
  263.     /* Close the cancel dialog and re-enable main app. window */
  264.     EnableWindow (hwndFrame, TRUE);
  265.     DestroyWindow (hwndPDlg);
  266.  
  267. getout1:
  268.     DeleteDC(hdc);
  269.  
  270. getout5:
  271.     /* Get rid of dialog procedure instances */
  272.     FreeProcInstance (lpfnPDlg);
  273.  
  274. getout4:
  275.     FreeProcInstance (lpfnAbort);
  276.  
  277. getout:
  278.  
  279.     /* Error? make sure the user knows... */
  280.     if (fError)
  281.     MPError (hwnd, MB_OK | MB_ICONEXCLAMATION, IDS_PRINTERROR, (LPSTR)szTitle);
  282.  
  283.     return;
  284. }
  285.  
  286. #if 0
  287.  
  288. /****************************************************************************
  289.  *                                        *
  290.  *  FUNCTION   : GetInitializationData()                    *
  291.  *                                        *
  292.  *  PURPOSE    : Gets DC initialization data from a printer driver        *
  293.  *         supporting ExtDeviceMode(). Called in response to the        *
  294.  *         File/Printer setup menu selection.                *
  295.  *                                        *
  296.  *         This function allows the user to change the printer        *
  297.  *         settings FOR MULTIPAD ONLY.  This allows Multipad to print *
  298.  *         in a variety of settings without messing up any other        *
  299.  *         applications. In a more sophisticated application, this    *
  300.  *         setup could even be saved on a document-by-document basis. *
  301.  *                                        *
  302.  ****************************************************************************/
  303. BOOL FAR PASCAL GetInitializationData( hwnd )
  304. HWND hwnd ;
  305. {
  306.     LPSTR     lpOld;
  307.     LPSTR     lpNew;
  308.     FARPROC   lpfn;
  309.     HANDLE    hT,hDrv;
  310.     char      sz[32];
  311.     WORD      cb;
  312.     int       flag;
  313.  
  314.     /* Pop up dialog for user and retain data in app buffer */
  315.     flag = DM_PROMPT | DM_COPY;
  316.  
  317.     /* Load the device driver and find the ExtDeviceMode() function */
  318.     wsprintf (sz, "%s.drv", (LPSTR)szDriver);
  319.     if ((hDrv = LoadLibrary (sz)) < 32)
  320.     return FALSE;
  321.     if (!(lpfn = GetProcAddress (hDrv, szExtDeviceMode)))
  322.     return FALSE;
  323.  
  324.     if (hInitData){
  325.     /* We have some old data... we want to modify the previously specified
  326.      * setup rather than starting with the default setup.
  327.      */
  328.     lpOld = (LPSTR)LocalLock(hInitData);
  329.     flag |= DM_MODIFY;
  330.     }
  331.     else
  332.     lpOld = NULL;
  333.  
  334.     /* Get the number of bytes needed for the init data */
  335.     cb = (*lpfn) (hwnd,
  336.           hDrv,
  337.           NULL,
  338.           (LPSTR)szDevice,
  339.           (LPSTR)szPort,
  340.           (LPDEVMODE)NULL,
  341.           (LPSTR)NULL,
  342.           0);
  343.  
  344.     /* Grab some memory for the new data and lock it. */
  345.     hT      = LocalAlloc (LHND,cb);
  346.     lpNew = (LPSTR)LocalLock (hT);
  347.  
  348.     /* Post the device mode dialog. 0 flag iff user hits OK button */
  349.     if ((*lpfn) (hwnd,
  350.          hDrv,
  351.          (LPDEVMODE)lpNew,
  352.          (LPSTR)szDevice,
  353.          (LPSTR)szPort,
  354.          (LPDEVMODE)lpOld,
  355.          (LPSTR)NULL,
  356.          flag)==IDOK)
  357.     flag = 0;
  358.  
  359.     /* Unlock the input structures */
  360.     LocalUnlock (hT);
  361.     if (hInitData)
  362.     LocalUnlock (hInitData);
  363.  
  364.     /* If the user hit OK and everything worked, free the original init.
  365.      * data and retain the new one.  Otherwise, toss the new buffer.
  366.      */
  367.     if (flag)
  368.     LocalFree (hT);
  369.     else{
  370.     if (hInitData)
  371.         LocalFree (hInitData);
  372.     hInitData = hT;
  373.     }
  374.  
  375.     FreeLibrary(hDrv);
  376.     return (!flag);
  377. }
  378.  
  379.  
  380. #endif
  381. 
  382.